<?php

namespace app\web\controller;

use app\common\controller\Web;
use app\common\model\AlgorithmCaseModel;
use app\common\model\AlgorithmItemIndustryModel;
use app\common\model\AlgorithmSector;
use app\common\model\AlgorithmItemOther;
use app\common\model\Algorithm;
use app\common\model\AlgorithmOther;
use app\common\model\AlgorithmQuestionModel;
use app\common\model\AlgorithmSceneModel;
use app\common\model\Apply;
use app\common\model\CasesItemModel;
use app\common\model\Link;

/**
 * 算法商城
 * User: wufeng
 * Date: 2020/06/24.
 */
class Marketplace extends Web
{
	//算法商城
	public function lists()
	{
		$id = $this->request->param('id');
		$this->assign('algorithm_id', $id);
		return view('lists');
	}

	public function detail()
	{
		$id = $this->request->param('id');
		$algorithmModel = new Algorithm();
		$algorithmSectorModel = new AlgorithmSector();
		$algorithmOtherModel = new AlgorithmOther();
		if (isset($id)) {
			//热门推荐算法
			$algorithmList = $algorithmModel->where(['is_hot' => 1])->select();
			$algorithm = $algorithmModel->where('id', 'eq', $id)->find();
			$algorithm['pic'] = _sImage($algorithm['pic']);
			$sector = $algorithmSectorModel->where('id', 'eq', $algorithm['sector_id'])->find();
			//应用场景
			$sceneList = $algorithmOtherModel->where(['algorithm_id' => $id, 'type' => 1])->limit(3)->select();
			if (! $sceneList->isEmpty()) {
				foreach ($sceneList as $item){
					$item['pic'] = _sImage($item['pic']);
				}
				$scene = $sceneList->toArray();
			} else {
				$scene = [];
			}
			//常见问题
			$questionList = $algorithmOtherModel->where(['type' => 2])->select();
			if (! $questionList->isEmpty()) {
				foreach ($questionList as $item){
					$item['pic'] = _sImage($item['pic']);
				}
				$question = $questionList->toArray();
			} else {
				$question = [];
			}
			//相关实例
			$exampleList = $algorithmOtherModel->where(['algorithm_id' => $id, 'type' => 3])->limit(4)->select();
			if (! $exampleList->isEmpty()) {
				foreach ($exampleList as $item){
					$item['pic'] = _sImage($item['pic']);
					$item['logo'] = _sImage($item['logo']);
					$sectorInfos = json_decode($item['sector_info']);
					$sector = [];
					foreach ($sectorInfos as $key => $info){
						$sector[$key]['id'] = $info;
						$sector[$key]['title'] = (new AlgorithmSector())->get($info)['title'];
					}
					$item['sector'] = $sector;
				}
				$example = $exampleList->toArray();
			} else {
				$example = [];
			}

			$employ = (new Link())->get(['position' => 2]);
			$this->assign('employ', $employ);

			$this->assign('algorithm', $sector);
			$this->assign('item', $algorithm);
			$this->assign('scene', $scene);
			$this->assign('question', $question);
			$this->assign('example', $example);
			$this->assign('algorithmList', $algorithmList);
		} else {
			return [
				'code' => -1,
				'msg'  => '缺少参数id'
			];
		}
		return view('detail');
	}

	public function search()
	{
		$param = $this->request->param();
		dump($param);exit();
	}
}
